define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/auth/databaselog/index' + location.search,
                    edit_url: 'xiluhouse/auth/databaselog/edit',
                    del_url: 'xiluhouse/auth/databaselog/del',
                    multi_url: 'xiluhouse/auth/databaselog/multi',
                    user_url:'xiluhouse/user/user/index',
                    admin_url:'auth/admin/index',
                    table: 'database_log',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.user_url).data("primaryKey", "id").data("field", "mobile");
                $("input[name='admin_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.admin_url).data("primaryKey", "id").data("field", "username");
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable:true},
                        {field: 'user_id', title: __('User_id'),formatter: function (value, row, index) {
                            if(row.user_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.user_url+ "?id=" + row.user_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+ row.user_id +'】'+row.user.nickname+'<br/>'+row.user.mobile+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }
                        },
                       {field: 'admin_id', title: __('admin_id'),formatter: function (value, row, index) {
                            if(row.admin_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.admin_url+ "?id=" + row.admin_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> 【'+ row.admin_id +'】'+row.admin.username+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }
                        },
                        {field: 'type', title: __('Type'), searchList: {"after_delete":__('After_delete'),"after_update":__('After_update'),"after_insert":__('After_insert')}, formatter: Table.api.formatter.normal},
                        {field: 'url', title: __('Url'), formatter: Table.api.formatter.url},
                        {field: 'ip', title: __('Ip')},
                        {field: 'model_name', title: __('Model_name')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});